<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="订单号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-popup v-decorator="['orderCode']" :trigger-change="true" org-fields="order_code"
            dest-fields="orderCode" code="purchaseOrder_search" @callback="popupCallback" />
        </a-form-item>
        <a-form-item label="日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择日期" v-decorator="['birDay']" :trigger-change="true" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="客户料号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['clientCode']" placeholder="请输入客户料号"></a-input>
        </a-form-item>
        <a-form-item label="物料料号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['materielCode', validatorRules.materielCode]" placeholder="请输入物料料号"></a-input>
        </a-form-item>
        <a-form-item label="品名" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['materielName', validatorRules.materielName]" placeholder="请输入品名"></a-input>
        </a-form-item>
        <a-form-item label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['materielSpecs', validatorRules.materielSpecs]" placeholder="请输入规格"></a-input>
        </a-form-item>
        <a-form-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['unit', validatorRules.unit]" placeholder="请输入单位"></a-input>
        </a-form-item>
        <a-form-item label="退料数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['retreatNum', validatorRules.retreatNum]" placeholder="请输入退料数量"></a-input>
        </a-form-item>
        <a-form-item label="实收数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['uretreatNum', validatorRules.uretreatNum]" placeholder="请输入实收数量"></a-input>
        </a-form-item>
        <a-form-item label="箱数" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['xsNum']" placeholder="请输入箱数"></a-input>
        </a-form-item>
        <a-form-item label="毛重（KG）" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['mzNum']" placeholder="请输入毛重（KG）"></a-input>
        </a-form-item>
<!--        <a-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
<!--          <a-select v-decorator="['state', validatorRules.state]" @change="getState">-->
<!--            <a-select-option value="0">质检前</a-select-option>-->
<!--            <a-select-option value="1">质检后</a-select-option>-->
<!--          </a-select>-->
<!--        </a-form-item>-->
        <a-form-item label="破损" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['reason1']" placeholder="请输入数量"></a-input>
        </a-form-item>
        <a-form-item label="断裂" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['reason2']" placeholder="请输入数量"></a-input>
        </a-form-item>
        <a-form-item label="划伤" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['reason3']" placeholder="请输入数量"></a-input>
        </a-form-item>
        <a-form-item label="来料损件" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['reason4']" placeholder="请输入数量"></a-input>
        </a-form-item>
        <a-form-item label="尺寸不符" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['reason5']" placeholder="请输入数量"></a-input>
        </a-form-item>
        <a-form-item label="功能不良" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['reason6']" placeholder="请输入数量"></a-input>
        </a-form-item>
        <a-form-item label="实物与焊盘不符" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['reason7']" placeholder="请输入数量"></a-input>
        </a-form-item>
        <a-form-item label="料号不符" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['reason8']" placeholder="请输入数量"></a-input>
        </a-form-item>
        <a-form-item label="线束不良" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['reason9']" placeholder="请输入数量"></a-input>
        </a-form-item>
        <a-form-item label="型号不符" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['reason10']" placeholder="请输入数量"></a-input>
        </a-form-item>
        <a-form-item label="其它原因" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['reason11']" placeholder="请输入原因及数量"></a-input>
        </a-form-item>
        <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['remarks']" placeholder="请输入备注"></a-input>
        </a-form-item>

      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'

  export default {
    name: "MaterielRetreatRecordModal",
    components: {
      JDate,
    },
    props:{
      showMore:{
        type:Boolean,
        default:false
      }
    },
    data () {
      return {
        state:'0',
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules: {
          materielCode: {rules: [{required: true, message: '请输入物料料号!'}]},
          materielName: {rules: [{required: true, message: '请输入品名!'}]},
          materielSpecs: {rules: [{required: true, message: '请输入规格!'}]},
          unit: {rules: [{required: true, message: '请输入单位!'}]},
          retreatNum: {rules: [{required: true, message: '请输入退料数量!'}]},
          uretreatNum: {rules: [{required: true, message: '请输入实收数量!'}]},
          state: {rules: [{required: true, message: '请选择状态!'}]},
        },
        url: {
          add: "/inspect/materielRetreatRecord/add",
          edit: "/inspect/materielRetreatRecord/edit",
        }

      }
    },
    created () {
    },
    methods: {
      getState(){
        this.$nextTick(() => {
          this.state = this.form.getFieldValue('state')
        });
      },
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'orderCode','birDay','clientCode','materielCode','materielName','materielSpecs','unit','retreatNum','uretreatNum','xsNum','mzNum','state','remarks',
            'reason1','reason2','reason3','reason4','reason5','reason6','reason7','reason8','reason9','reason10','reason11'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'orderCode','birDay','clientCode','materielCode','materielName','materielSpecs','unit','retreatNum','uretreatNum','xsNum','mzNum','state','remarks',
          'reason1','reason2','reason3','reason4','reason5','reason6','reason7','reason8','reason9','reason10','reason11'))
      },


    }
  }
</script>